package com.heba.adapter.diary;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.heba.bean.diary.ItemImageTagObject;
import com.heba.bean.diary.ItemUploadPhotoObject;
import com.heba.config.CommonConfig;
import com.heba.greenmulu.R;
import com.heba.greenmulu.message.ItemMessageSendActivity;
import com.heba.utils.DensityUtils;
import com.heba.utils.ImageTools;
import com.heba.view.DiaryImageTagLayout;

import java.util.List;

/**
 * 圖片標記用戶adapter
 * <p>
 * Created by xingbao on 2019/2/12.
 */

public class DiaryTagPhotoPagerAdapter extends PagerAdapter {

    private Context context;
    private List<ItemUploadPhotoObject> list;

    private int width;  //图片显示的宽度

    public DiaryTagPhotoPagerAdapter(Context context, List<ItemUploadPhotoObject> list) {
        this.context = context;
        this.list = list;
        this.width = DensityUtils.getWidth(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_diary_image_tag, null);
        DiaryImageTagLayout layoutContent = view.findViewById(R.id.layoutContent);

        ItemUploadPhotoObject object = list.get(position);


        /**
         * todo 根据图片的比例 计算图片显示的实际高度
         */
        //显示图片
        float scale = getImageScale(object);
        int height = (int) (width * scale);
        layoutContent.setImgBg(width, height, object);
        object.setScale(scale);
        object.setViewWidth(width);
        object.setViewHeight(height);

        //设置监听
        layoutContent.setOnImageClickListener(new DiaryImageTagLayout.ImageTagListener() {
            @Override
            public void onAddTagClick(int x, int y) {
                /**
                 * 添加用户标签
                 */
                Intent intent = new Intent(context, ItemMessageSendActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(CommonConfig.TYPE, CommonConfig.TYPE_IMAGE_TAG);
                bundle.putInt(CommonConfig.POSITION, position);
                bundle.putInt(CommonConfig.TAG_IMAGE_POS_X, x);
                bundle.putInt(CommonConfig.TAG_IMAGE_POS_Y, y);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }

            @Override
            public void onTagConfirmPosition(List<ItemImageTagObject> imageTagObjects) {
                /**
                 * 保存所有标签的X、Y轴位置和比例
                 */
                for (ItemImageTagObject tagObject : imageTagObjects) {
                    float scaleX = (float) tagObject.getX() / object.getViewWidth();
                    float scaleY = (float) tagObject.getY() / object.getViewHeight();
                    tagObject.setScaleX(scaleX);
                    tagObject.setScaleY(scaleY);
                }
            }
        });

        container.addView(view);

        return view;
    }

    /**
     * 刷新数据
     *
     * @param uploadPhotoObjectList
     */
    public void updateData(List<ItemUploadPhotoObject> uploadPhotoObjectList) {
        this.list = uploadPhotoObjectList;
        notifyDataSetChanged();
    }

    /**
     * 获取图片缩放比例
     *
     * @param object
     * @return
     */
    private float getImageScale(ItemUploadPhotoObject object) {
        float scale;
        //获取图片实际宽高
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(object.getPhotoPath(), options);
        int imageWidth = options.outWidth;
        int imageHeight = options.outHeight;

        //计算图片旋转角度
        int orientation = ImageTools.getImageOrientation(object.getPhotoPath());
        switch (orientation) {
            //判断图片是否被旋转
            case ExifInterface.ORIENTATION_ROTATE_90:
            case ExifInterface.ORIENTATION_ROTATE_270:
                object.setImageWidth(imageHeight);
                object.setImageHeight(imageWidth);
                scale = (float) imageWidth / (float) imageHeight;
                break;
            default:
                object.setImageWidth(imageWidth);
                object.setImageHeight(imageHeight);
                scale = (float) imageHeight / (float) imageWidth;
        }

        return scale;
    }

}
